<?php
class Book extends My_Controller{
	
	public static  $m_Type = array(
			DOUBAN_TYPE,
	);
	
	public static $m_deep = 100; // 假设一百张A4纸张 是1厘米厚
	
	public $doubanID = null;
	
	public function __construct()
	{
		parent::__construct();
	
		$this->doubanID = $this->input->get('id');
		$this->load->model('douban/douban_book_m');

		if (empty($this->m_arrUser))
		{
			if (!$this->doubanID )
			{
				// 未登录 + 没有ID
				redirect('account/login');
			}
			else {
				if (!$this->douban_book_m->isUserExist($this->doubanID))
				{
					// 未登录 + ID 未找到 ..
					redirect('account/login');
				}
				else {
	                // TODO: 权限判断 .. 判断用户是否要让好友得知
              
				}
			}
		}
		$this->load->library('my_api',self::$m_Type);
	
	}
	
	public function index()
	{
		
		// 未登录 或则  有目标ID 
    	if (empty($this->m_arrUser) || $this->doubanID != 0)
    	{
    		$this->data['doubanUserBook'] = $this->douban_book_m->get($this->doubanID);
    	}
    	else // 登录 且没有明确目标
    	{
    		$this->load->library('my_api',self::$m_Type);
    		
    		// 获取个人用户个人信息
    		$data = array(
    				'id' => $this->my_api->douban_uid,
    		);
    		
    		$doubanUserInfo  = $this->my_api->douban->api('User.info.GET',$data)->makeRequest();
    		
    		
    		// 获取用户的所有读书信息  最大值为100
    		
    		$bigReadBookList = array();
    		$bookPageTotal   = 0;
    		$bookRateTotal   = 0;
    		$bookRateNums    = 0;
    		$data = array(
    				'name' => $doubanUserInfo['uid'],
    				'status' =>'read',
    				'start' => 0,
    				'count' => 100,
    		);
    		
    		$bookList  = $this->my_api->douban->api('Book.collections.GET',$data)->makeRequest();
    		
    		$bigReadBookList['total'] = $bookList['total'];
    		
    		foreach ($bookList['collections'] as $book)
    		{
    			$bigReadBookList['list'][] = $book;
    		
    			$bookPageTotal += $book['book']['pages'];
    			if (isset($book['rating']))
    			{
    				$bookRateTotal += $book['rating']['value'];
    		
    				if ($book['rating']['value'] > 0 && $book['rating']['value'] <6)
    				{
    					$bookRateNums ++;
    				}
    			}
    		
    		}
    		
    		$page = 1;
    		if ($bigReadBookList['total'] > 100)
    		{
    			$page = ((int)$bigReadBookList['total']/100)+1;
    		}
    		
    		for ($i = 1;$i <$page;$i++)
    		{
    		$data = array(
    				'name' => $doubanUserInfo['uid'],
    				'status' =>'read',
				    'start' => $i*100+1,
    				'count' => 100,
    		);
    		
    		$bookList  = $this->my_api->douban->api('Book.collections.GET',$data)->makeRequest();
    							
    		foreach ($bookList['collections'] as $book)
			{
    						$bigReadBookList['list'][] = $book;
    						$bookPageTotal += $book['book']['pages'];
    		
    						if (isset($book['rating']))
    				{
    				$bookRateTotal += $book['rating']['value'];
    		
    				if ($book['rating']['value'] > 0 && $book['rating']['value'] <6)
    				{
    					$bookRateNums ++;
    				}
    				}
		}
    				}
    		
    						$data = array(
    					'name' => $doubanUserInfo['uid'],
    					'start' => 0,
    					'count' => 1000,
    				);
    		
    				$bigTagList  = $this->my_api->douban->api('Book.userTags.GET',$data)->makeRequest();
    		
    		
    				$this->data['book'] = array(
    						'list' =>$bigReadBookList['list'],
    				'totalPage' => $bookPageTotal,
				'totalRateNums' => $bookRateNums,
						'avRate' => $bookRateTotal/$bookRateNums,
    										'total' => $bigReadBookList['total'],
    		);
    		
    		// 写入mongodb
    		//$this->douban_book_m->addData($this->my_api->douban_uid,$this->data['book']);
    		
    		$isExist = $this->douban_book_m->isUserExist($this->my_api->douban_uid);
    		
    		if ($isExist === false)
    		{
    		$this->douban_book_m->addData($this->my_api->douban_uid,$this->data['book']);
    		}
    	}
		
		
		$this->load->view('douban/live/book',$this->data);
	}
}